package com.ruoyi.whzj.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.whzj.domain.WhBalancerecord;
import com.ruoyi.whzj.service.IWhBalancerecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 余额变动Controller
 * 
 * @author ruoyi
 * @date 2020-10-21
 */
@Controller
@RequestMapping("/whzj/balancerecord")
public class WhBalancerecordController extends BaseController
{
    private String prefix = "whzj/balancerecord";

    @Autowired
    private IWhBalancerecordService whBalancerecordService;

    @RequiresPermissions("whzj:balancerecord:view")
    @GetMapping()
    public String balancerecord()
    {
        return prefix + "/balancerecord";
    }

    /**
     * 查询余额变动列表
     */
    @RequiresPermissions("whzj:balancerecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WhBalancerecord whBalancerecord)
    {
        startPage();
        List<WhBalancerecord> list = whBalancerecordService.selectWhBalancerecordList(whBalancerecord);
        return getDataTable(list);
    }

    /**
     * 查询代理佣金列表
     */
    @RequiresPermissions("whzj:balancerecord:list")
    @PostMapping("/shareRecodeList")
    @ResponseBody
    public TableDataInfo shareRecodeList(WhBalancerecord whBalancerecord)
    {
        startPage();
        List<WhBalancerecord> list = whBalancerecordService.selectSharerecordList(whBalancerecord);
        return getDataTable(list);
    }

    /**
     * 导出余额变动列表
     */
    @RequiresPermissions("whzj:balancerecord:export")
    @Log(title = "余额变动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WhBalancerecord whBalancerecord)
    {
        List<WhBalancerecord> list = whBalancerecordService.selectWhBalancerecordList(whBalancerecord);
        ExcelUtil<WhBalancerecord> util = new ExcelUtil<WhBalancerecord>(WhBalancerecord.class);
        return util.exportExcel(list, "balancerecord");
    }

    /**
     * 新增余额变动
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增余额变动
     */
    @GetMapping("/sharerecord")
    public String sharerecord()
    {
        return prefix + "/sharerecord";
    }

    /**
     * 新增保存余额变动
     */
    @RequiresPermissions("whzj:balancerecord:add")
    @Log(title = "余额变动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WhBalancerecord whBalancerecord)
    {
        return toAjax(whBalancerecordService.insertWhBalancerecord(whBalancerecord));
    }

    /**
     * 修改余额变动
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        WhBalancerecord whBalancerecord = whBalancerecordService.selectWhBalancerecordById(id);
        mmap.put("whBalancerecord", whBalancerecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存余额变动
     */
    @RequiresPermissions("whzj:balancerecord:edit")
    @Log(title = "余额变动", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WhBalancerecord whBalancerecord)
    {
        return toAjax(whBalancerecordService.updateWhBalancerecord(whBalancerecord));
    }

    /**
     * 删除余额变动
     */
    @RequiresPermissions("whzj:balancerecord:remove")
    @Log(title = "余额变动", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(whBalancerecordService.deleteWhBalancerecordByIds(ids));
    }

    /**
     * 查询汇总
     */
    @RequiresPermissions("whzj:balancerecord:list")
    @PostMapping("/getCount")
    @ResponseBody
    public AjaxResult getCount(String type,String userName, String operate,String startTime,String endTime,String yongjinUserName) throws ParseException {
        Map<String,Object> resultMap = new HashMap<>();
        WhBalancerecord whBalancerecord = new WhBalancerecord();
        whBalancerecord.setUserName(userName);
        whBalancerecord.setOperate(operate);
        whBalancerecord.setYongjinUserName(yongjinUserName);
        Map<String,Object> params = new HashMap<>();
        params.put("beginDdate",startTime);
        params.put("endDdate",endTime);
        whBalancerecord.setParams(params);
        List<WhBalancerecord> list = new ArrayList<>();
        if("1".equals(type)){
            list = whBalancerecordService.selectWhBalancerecordList(whBalancerecord);
        }
        if("2".equals(type)){
            list = whBalancerecordService.selectSharerecordList(whBalancerecord);
        }
        BigDecimal sumMoney = new BigDecimal("0.0");
        for(WhBalancerecord temp:list){
            sumMoney = sumMoney.add(temp.getBalance());
        }

        resultMap.put("sumMoney",sumMoney);
        return  new AjaxResult(AjaxResult.Type.SUCCESS,"200",resultMap);
    }
}
